package com.seo.textgen.pos.keyword;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.seo.textgen.pos.tagger.EndOfSentenceDetector;
import com.seo.textgen.pos.tagger.TaggedSentence;
import com.seo.textgen.pos.tagger.Token;
import com.seo.textgen.pos.tagger.TreeTagger;

public class KeywordTagger {

	private static final String KEYWORD_SEPARATOR = "/**/";

	private static final String KEYWORD_SEPARATOR_WITH_SPACES = " " + KEYWORD_SEPARATOR + " ";

	private TreeTagger treeTagger = new TreeTagger();
	
	public KeywordTagger() {
		treeTagger.setEndOfSentenceDetector(new EndOfSentenceDetector() {
			public boolean isEndOfSentence(Token token) {
				return KEYWORD_SEPARATOR.equals(token.getToken());
			}
		});
	}

	public TaggedKeyword[] performKeywordTagging(String[] keywords) throws IOException {
		String joinedKeywords = StringUtils.join(keywords, KEYWORD_SEPARATOR_WITH_SPACES);
		List<TaggedSentence> taggedSentences = treeTagger.performPosTagging(joinedKeywords);
		TaggedKeyword[] taggedKeywords = new TaggedKeyword[taggedSentences.size()];
		
		int i = 0;
		for (TaggedSentence taggedSentence : taggedSentences) {
			List<Token> tokens = taggedSentence.getTokens();
			tokens = tokens.subList(0, tokens.size() - 1);
			List<Object> parts = taggedSentence.getParts();
			parts = parts.subList(0, parts.size() - 2);
			taggedKeywords[i] = new TaggedKeyword(parts, tokens, keywords[i]);
			i++;
		}
		
		return taggedKeywords;
	}
}
